<template>
  <CustomNav />

  <!-- <IndexSkeleton v-if="loading" /> -->

  <view>
    <!-- 滚动容器 -->
    <scroll-view
      refresher-enabled
      @refresherrefresh="onRefresherrefresh"
      :refresher-triggered="isTriggered"
      scroll-y
      @scrolltolower="onScrolltolower"
      class="scroll-view"
    >
      <EsSwiper :swiperList="bannerList" />
      <!-- 分类 -->
      <IndexCateGory :CateGoryList="CateGoryList" />
      <!-- 热门 -->
      <IndexHotShop :HotShopList="HotShopList" />
      <!-- 猜你喜欢 -->
      <EsListMore ref="guessRef" />
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'

import { http } from '@/utils/http'
import CustomNav from '@/pages/index/components/CustomNav.vue'
// import IndexSkeleton from '@/pages/index/components/IndexSkeleton.vue'
import IndexCateGory from '@/pages/index/components/IndexCateGory.vue'
import IndexHotShop from '@/pages/index/components/IndexHotShop.vue'

import { getHomeBannerAPI, getHomeCateGoryListAPI, getHomeHotShopAPI } from '@/api/home'
import type { BannerItem, CateGoryItem, HotShopItem } from '@/types/home.d.ts'
import type { EsListMoreInstance } from '@/types/components'
// 获取猜你喜欢组件实例
const guessRef = ref<EsListMoreInstance>()
// 下拉刷新状态
const isTriggered = ref(false)
const loading = ref(true)
// 滚动触底事件
const onScrolltolower = () => {
  guessRef.value?.getMore()
}
// 获取banner图列表
let bannerList = ref<Array<BannerItem>>([])
const getHomeBanner = async () => {
  let res = await getHomeBannerAPI()
  bannerList.value = res.result as Array<BannerItem>
  loading.value = false
}
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData() // 加载数据
  await Promise.all([getHomeBanner()]) // 关闭动画
  isTriggered.value = false
}
let listRef = []
// 获取gateGoryList
let CateGoryList = ref<Array<CateGoryItem>>([])
const getHomeCateGoryList = async () => {
  let res = await getHomeCateGoryListAPI()
  CateGoryList.value = res.result as Array<CateGoryItem>
}
// 获取HotShopList
let HotShopList = ref<Array<HotShopItem>>([])
const getHotShopList = async () => {
  let res = await getHomeHotShopAPI()
  HotShopList.value = res.result as Array<HotShopItem>
}
onLoad(() => {
  getHomeBanner()
  getHomeCateGoryList()
  getHotShopList()
})
</script>

<style>
page {
  /* box-sizing: border-box; */
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #f7f7f7;
}
.scroll-view {
  flex: 1;
  overflow: auto;
}
</style>
